//index.js
//获取应用实例
const app = getApp()

import * as API from "../../utils/config.js"
Page({
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    circular: true,
    catList:[],
    list:{},
  },
  onLoad: function (options) {
    this._initData()
  },
  onReady:function(){
    if(!wx.getStorageSync("list")){
      this.getData()
    }else{
      wx.getStorage({
        key: 'list',
        success: (res)=>{
          // success
          // console.log(res)
          this.setData({
            "list":res.data
          })
        }
      })
    }
    
    
  },
  _initData() {
    wx.request({
      url: API.comics,
      success: (res) => {
        // console.log(res.data.data)
        this.setData({
          Imagelist: res.data.data
        })
      }
    })
  },
  getData:function(){
    let self = this;
    let data = {};
    wx.request({
      url:API.classify,
      // data:{
      //   "key":"b54e80c6032ede35cfe0cc5ea68dc827"
      // },
      // url: 'http://japi.juhe.cn/comic/category?key=b54e80c6032ede35cfe0cc5ea68dc827',
      success: (res)=>{
        // success
        //获取分类列表
        // console.log(res);
        let categoryList = res.data.result
        categoryList.forEach((value,index) => {
          wx.request({
            url: "https://easy-mock.com/mock/5ae18553ebc6c061022b9a7f/comic/book?type="+value,
            data: {},
            success: function(res){
              // success
              // console.log(res.data.data.result.bookList)
              data[value] = res.data.data.result.bookList;
              self.data.list[value] = res.data.data.result.bookList
              wx.setStorage({
                key:"list",
                data:self.data.list
              })
              self.setData({
                list:self.data.list
              })
            }
          })
        });
      }
    })
    // console.log(data);
    

  },
})